//
//  RegiestViewController.m
//  Project_IOS
//
//  Created by Jeff on 2022/1/18.
//

#import "RegiestViewController.h"
#import "RegistNumberView.h"
#import "LoginNumberView.h"
#import "LoginAgreementView.h"
#import <Masonry/Masonry.h>
#import <MBProgressHUD/MBProgressHUD.h>
#import "UIColor+HEX.h"
#import "LoginTools.h"
#import "Color.h"
@interface RegiestViewController ()

@property (nonatomic, strong) RegistNumberView *registNumberView;
@property (nonatomic, strong) LoginNumberView *pwView;
@property (nonatomic, strong) LoginAgreementView *agreementV;
@property (nonatomic, strong) UIButton *fastBtn;

@end

@implementation RegiestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleStr = @"欢迎注册";
    self.buttonView.btnStr = @"注册";
    
    self.registNumberView = [[RegistNumberView alloc] init];
//    self.registNumberView.textField.delegate = self;
    [self.registNumberView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.pwView = [[LoginNumberView alloc] initPhone:false];
    [self.pwView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:self.registNumberView];
    [self.registNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(110);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    [self.contentView addSubview:self.pwView];
    [self.pwView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.registNumberView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    self.agreementV = [LoginAgreementView new];
    [self.contentView addSubview:self.agreementV];
    [self.agreementV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(311, 30));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.pwView.mas_bottom).offset(5);
    }];
    
    [self.contentView addSubview:self.buttonView];
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(311, 48));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.agreementV.mas_bottom).offset(20);
    }];
    
    [self.buttonView.button addTarget:self action:@selector(loginBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    self.buttonView.avable = false;
    
    UILabel *underlineLabel = [[UILabel alloc] init];
    underlineLabel.font = [UIFont systemFontOfSize:12];
    NSString *textStr = @"已有账号？登录";
    underlineLabel.textColor = [Color colorWithHexString:@"#999999"];
    // 下划线
    //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName : [Color colorWithHexString:@"#2075FD"],NSForegroundColorAttributeName:[Color colorWithHexString:@"#2075FD"]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr];
    [attribtStr addAttributes:attribtDic range:NSMakeRange(5, 2)];
    //赋值
    underlineLabel.attributedText = attribtStr;
    
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelClick)];
    
    [underlineLabel addGestureRecognizer:gestureRecognizer];
    underlineLabel.userInteractionEnabled = YES;
    
    [self.contentView addSubview:underlineLabel];
    [underlineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.buttonView.mas_bottom).offset(30);
        make.centerX.mas_equalTo(0);
    }];
    
    self.fastBtn.hidden = LoginTools.currentLoginUser.is_guest != YES;
    [self.contentView addSubview:self.fastBtn];
    [self.fastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.buttonView);
        make.top.equalTo(underlineLabel.mas_bottom).offset(10);
    }];
}

- (void)labelClick {
    [self.navigationController pushViewController:[LoginTools creatLoginVC] animated:YES];
}

- (void)loginBtnOnClick {
    if (!self.agreementV.btn.isSelected) {
//        [MBProgressHUD showMessage:@"请勾选同意《用户协议》和《隐私政策》！"];
        return;
    }
    [self.registNumberView.textField resignFirstResponder];
    [self.pwView.textField resignFirstResponder];
    [LoginTools login:self.registNumberView.textField.text pwd:self.pwView.textField.text success:^{
        if (self.loginCallback) {
            self.loginCallback(YES);
        }
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    return true;
//}

- (void)textfieldContentChange:(UITextField *)field {
    if (self.registNumberView.textField.text.length == 11 && self.pwView.textField.text != nil && self.pwView.textField.text.length > 0) {
        self.buttonView.avable = true;
    }else{
        self.buttonView.avable = false;
    }
}

- (UIButton *)fastBtn {
    if (!_fastBtn) {
        _fastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fastBtn setTitle:@"已有游客会员账号，点击一键登录" forState:UIControlStateNormal];
        [_fastBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _fastBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_fastBtn addTarget:self action:@selector(actionFastLogin) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fastBtn;
}

- (void)actionFastLogin {
    [LoginTools fastLogin:^{
        if (self.loginCallback) {
            self.loginCallback(YES);
        }
        [self dismiss];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
